package app.textSrc;

import java.io.IOException;
import java.util.Properties;

public class TextSource {
	private Properties queProp;
	private String[] cusAnsStr;
	public static final String ansLetStr = "ABCD";
	private final String[] keys = { "AmountOfQuestion", "pathFileMp3" };

	public TextSource(Properties queProp) throws IOException {
		if (queProp != null) {
			for (int i = 0; i < keys.length; i++) {
				if (!queProp.containsKey(keys[i])) {
					throw new IOException("Property file is missing key: "
							+ keys[i]);
				}
			}
			this.queProp = queProp;
			this.cusAnsStr = new String[getAmtQue()];
		} else {
			throw new IOException("ERROR -- Contstruct failed class: "
					+ getClass().getName());
		}
	}

	public int compareAnser() {
		int amtWroAns = 0;
		for (int i = 0; i < getAmtQue(); i++) {
			if (cusAnsStr[i] != null) {
				if (!cusAnsStr[i].equalsIgnoreCase(getCorAnsStr()[i])) {
					amtWroAns++;
				}
			} else {
				amtWroAns++;
			}
		}
		return amtWroAns;
	}

	public String[] getCusAnsStr() {
		return cusAnsStr;
	}

	public String[] getQueStr() {
		String[] queStr = new String[getAmtQue()];
		int textI = 0;
		for (int i = 0; i < getAmtQue(); i++) {
			textI = i + 1;
			queStr[i] = queProp.getProperty("q" + textI);
		}
		return queStr;
	}

	public String[][] getAnsStr() {
		String[][] ansStr = new String[getAmtQue()][4];
		int textI = 0;
		for (int i = 0; i < getAmtQue(); i++) {
			for (int j = 0; j < 4; j++) {
				textI = i + 1;
				ansStr[i][j] = queProp.getProperty("q" + textI
						+ ansLetStr.charAt(j));
			}
		}
		return ansStr;
	}

	public String[] getCorAnsStr() {
		String[] corAnsStr = new String[getAmtQue()];
		int textI = 0;
		for (int i = 0; i < getAmtQue(); i++) {
			textI = i + 1;
			corAnsStr[i] = queProp.getProperty("q" + textI + "ANS");
		}

		return corAnsStr;
	}

	public String getPathFilemp3() {
		String pathFileMp3 = queProp.getProperty(keys[1]);
		return pathFileMp3;
	}

	public String getPathFileTS(int index) {
		if (validIndex(index) && queProp != null) {
			return queProp.getProperty("TapScr" + String.valueOf(index));
		}
		return null;
	}

	public int getAmtQue() {
		int amtQue = Integer.parseInt(queProp.getProperty(keys[0]));
		return amtQue;
	}

	protected boolean validIndex(int index) {
		if (index > 0 && index < getAmtQue()) {
			return true;
		}

		return false;
	}
}
